import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:hitutor/UI/HiNote/MyDiary/MyDiary.dart';
import 'package:hitutor/UI/WebPage.dart';
import 'package:hitutor/Util/Global.dart';
import 'package:hitutor/Util/HttpGo.dart';
import 'package:hitutor/Util/MyColor.dart';
import 'package:hitutor/Util/PageUtil.dart';
import 'package:hitutor/Widget/MyBgPage.dart';
import 'package:hitutor/Widget/MyDialog.dart';
import 'package:hitutor/Widget/MyImage.dart';
import 'package:hitutor/Widget/MyPage.dart';

import 'Preview/PreviewList.dart';
import 'Review/ReviewList.dart';
import 'TeachMaterial/TeachMaterialList.dart';
import 'TeachMaterial/TeachMaterialManager.dart';

class HiNote extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _HiNote();
  }
}

class _HiNote extends State {
  bool showDiary = false;

  openLearningProcess() {
    HttpGo(context).get("/HiNote/GetLearningProcess", {}, (data) {
      PageUtil().open(context, WebPage("學習歷程 Learning Process", data["url"],showBrowser: true));
    }, (code, error) => showToast(error));
  }

  getShowDiary() {
    HttpGo(context).get("/Dairy/GetStudentDairyStatus?studentId=${Global.studentId}", {}, (data) {
      showDiary = data["status"];
      setState(() {});
    }, (code, error) => showToast(error));
  }

  @override
  void initState() {
    WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
      getShowDiary();
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MyBgPage(
        "HiNote",
        Container(
          width: double.infinity,
          child: SingleChildScrollView(
              child: Column(children: [
            ItemGreenBtn("book-close.png", "預習課程 Preview", onTap: () {
              PageUtil().open(context, PreviewList());
            }),
            ItemGreenBtn("book-open.png", "複習課程 Review", onTap: () {
              PageUtil().open(context, ReviewList());
            }),
            ItemOrangeBtn("folder.png", "教材專區 Teaching material", onTap: () {
              PageUtil().open(context, TeachMaterialList());
            }),
            ItemOrangeBtn("cloud-upload.png", "自備教材管理\nTeaching material management", onTap: () {
              PageUtil().open(context, TeachMaterialManager());
            }),
            Visibility(
                visible: showDiary,
                child: ItemOrangeBtn("diary.png", "我的日記 Diary", onTap: () {
                  PageUtil().open(context, MyDiary());
                })),
            ItemOrangeBtn("learning_process.png", "學習歷程 Learning Process", onTap: openLearningProcess)
          ])),
        ));
  }
}

class ItemGreenBtn extends StatelessWidget {
  String icon;
  String text;
  Function()? onTap;

  ItemGreenBtn(this.icon, this.text, {this.onTap});

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        onTap: onTap,
        child: Container(
            width: 260,
            margin: EdgeInsets.all(16),
            padding: EdgeInsets.all(12),
            decoration: Global.getWhiteBg_shadow(100, MyColor.DD),
            child: Column(mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [
              Container(
                  decoration: Global.getBg(Color(0xffe2f2f2), 24),
                  width: 48,
                  height: 48,
                  margin: EdgeInsets.only(bottom: 12),
                  alignment: Alignment.center,
                  child: LocalImage(icon, width: 24)),
              Text(text, style: TextStyle(color: Color(0xff43bacc)))
            ])));
  }
}

class ItemOrangeBtn extends StatelessWidget {
  String icon;
  String text;
  Function()? onTap;

  ItemOrangeBtn(this.icon, this.text, {this.onTap});

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
          width: 260,
          margin: EdgeInsets.all(16),
          padding: EdgeInsets.all(12),
          decoration: Global.getWhiteBg_shadow(100, MyColor.DD),
          child: Column(mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [
            Container(
                decoration: Global.getBg(Color(0xffffeade), 24),
                margin: EdgeInsets.only(bottom: 12),
                width: 48,
                height: 48,
                alignment: Alignment.center,
                child: LocalImage(icon, width: 24)),
            Text(text, textAlign: TextAlign.center, style: TextStyle(color: Color(0xfffa9f6b)))
          ])),
    );
  }
}
